//
//  JZHPartyCell.m
//  极致荟
//
//  Created by mac on 14-4-18.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHPartyCell.h"
#import "JZHACTview.h"
#import "JZHHowLookMeViewController.h"
#import "JZHHouViewController.h"
@implementation JZHPartyCell
JZHActivities *activit;
- (void)awakeFromNib
{
    [self.mianview.layer setCornerRadius:3.0];
    [self.mianview.layer setBorderWidth:1.0];
    [self.mianview.layer setBorderColor:[[UIColor grayColor] CGColor]];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        //加载nib
        NSArray *arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"JZHPartyCell" owner:self options:nil];
        if (arrayOfViews.count < 1) {
            return nil;
        }
        if (![[arrayOfViews objectAtIndex:0] isKindOfClass:[JZHPartyCell class]]) {
            return nil;
        }
        
        self = [arrayOfViews objectAtIndex:0];

    }
    [self.mianview.layer setCornerRadius:3.0];
    [self.mianview.layer setBorderWidth:1.0];
    [self.mianview.layer setBorderColor:[[UIColor colorWithRed:198/255.0 green:194/255.0 blue:192/255.0 alpha:1] CGColor]];
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (IBAction)puspin:(id)sender {
    [UINavigationController Pus:self.controller ToViewconeroller:[[JZHHouViewController alloc]initId:activit type:0]];
}
//查看参加该活动人数列表
- (void)partyList:(UITapGestureRecognizer *)tap
{
    [UINavigationController Pus:self.controller ToViewconeroller:[[JZHHowLookMeViewController alloc]initType:1 activit:activit]];
}
- (IBAction)pusShare:(id)sender {
    JZHShare *share= self.pusshare();
    UIImage *img=[UIImage imageNamed:@"sp1.png"];
    [share shareData:@"这是标题" shareTxt:@"这是内容" image:UIImageJPEGRepresentation(img, 0.5)];
}
- (IBAction)sky:(UISwitch *)sender {
   
     [self addpypo];
   //        if([User defuceUser].vip==0)
//        {
//            JZHACTview *act=[JZHACTview defut];
//            [act showViewtitle:@"需要成为黑卡会员才可以参加活动"];
//            act.ok=^(){
//                
//            };
//            
//            act.clos=^(){
//                sender.on=YES;
//                 self.partySignSwitch.enabled=YES;
//            };
//        }else
//        {
//            [self addpypo];
//        }
    
}

///报名
- (void)addpypo
{
    [MMProgressHUD showWithStatus:@"报名中..."];
    NSDictionary *dic=[[NSDictionary alloc]initWithObjectsAndKeys:[NSNumber numberWithInt:[User defuceUser].userId],@"userId", [NSNumber numberWithInt:activit.actId],@"partyId",nil];
    [HttpRequest httpRequestPost:JZHHTTPURL(@"party/addPartyJoin") parameters:dic block:^(id data, HttpReturnCode rerurncode) {
        NSLog(@"%@",data);
        if(rerurncode==0)
        {
            [MMProgressHUD dismissWithSuccess:@"报名成功"];
            self.partySignSwitch.on=YES;
            self.partySignSwitch.enabled=NO;
            activit.isjoin=YES;
        }else
        {
            [MMProgressHUD dismissWithError:@"报名失败" afterDelay:1.0];
            self.partySignSwitch.on=NO;
            self.partySignSwitch.enabled=YES;
        }
    }];
}
///给CELL添加数据
- (void)adddata:(JZHActivities *)act headpath:(NSString *)path
{
    activit=act;
    NSLog(@"%d  %d",act.consumptionMode,act.actBmType);
    self.partyTitle.text=act.acttile;
    self.userNameLabel.text=act.mainuser.name;
    [ImageCache imageUrl:act.mainuser.userHeadImage UiImageview:self.userHeaderImage];
    if(act.isjoin || [User  defuceUser].userId==act.mainuser.userId)
    {
        self.partySignSwitch.on=YES;
        self.partySignSwitch.enabled=NO;
    }else
    {
        self.partySignSwitch.on=NO;
        self.partySignSwitch.enabled=YES;
    }
    [self setagscroll:act.actimages path:path];
    [self addouserScollerImage:act.userarr];
    self.partyPersonNumberLabel.text=[NSString stringWithFormat:@"(%d人)",act.max];
    NSDateFormatter *form=[[NSDateFormatter alloc]init];
    [form setDateFormat:@"MM月dd日 hh:mm"];
    self.partyDateLabel.text=[form stringFromDate:act.actdate];
    self.partytype.text=[act actTypeStr];
    self.partyType.text=[act comsumptionMode];
    self.pace.text=act.addes;
    
}

///添加活动图片
- (void)setagscroll:(NSArray *)arr path:(NSString *)str
{
    
    NSLog(@"%@",arr);
    [self.partyImageScoll setContentSize:CGSizeMake(320*arr.count, 0)];
    for(int i=0;i<arr.count;i++)
    {
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(i*self.partyImageScoll.frame.size.width, 0, self.partyImageScoll.frame.size.width, self.partyImageScoll.frame.size.height)];
         NSString *path=@"";
        
        if([arr isKindOfClass:[NSArray class]])
        {
            path=[str stringByAppendingString:arr[i] ];
            NSLog(@"%@",path);
            [ImageCache imageUrl:path UiImageview:imageview];
            [self.partyImageScoll addSubview:imageview];
        }else
        {
            NSDictionary * dic=(NSDictionary *)arr;
            NSArray *imagearr=[dic allValues];
            path=[str stringByAppendingString:imagearr[i]];
            NSLog(@"%@",path);
            
            [ImageCache imageUrl:path UiImageview:imageview];
            [self.partyImageScoll addSubview:imageview];
        }
        
        //imageview.image=[UIImage imageNamed:@"1.jpg"];
//        [ImageCache imageUrl:path UiImageview:imageview];
//        [self.partyImageScoll addSubview:imageview];
    }
    
}

///添加参与者头像
- (void)addouserScollerImage:(NSArray *)arr
{
    if(arr.count!=0)
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(partyList:)];
            [self.partyImageScroller addGestureRecognizer:tap];
        });
    }
    for(int i=0;i<arr.count;i++)
    {
        if(i>7)
        {
            return;
        }
        
        UIImageView *imageview=[[UIImageView alloc]initWithFrame:CGRectMake(i*31+(i+1)*8, 0, 31, 31)];
        [ImageCache imageUrl:arr[i] UiImageview:imageview];
        [self.partyImageScroller addSubview:imageview];
    }
}
@end
